﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

namespace PortDetection
{
    public class WriteLog
    {
        static string filePath = Environment.CurrentDirectory;

        public static string FolderName = string.Empty;

        public static string LogName = string.Empty;
        public static void DataLog(string tag, string FolderName1 = "", string LogName1 = "")
        {
            string Folder = string.Empty;
            string Log = string.Empty;

            if (FolderName1 != "")
            {
                Folder = FolderName1;
            }


            if (LogName1 != "")
            {
                Log = LogName1;
            }


            if (Folder == string.Empty && FolderName == string.Empty)
            {
                Folder = "LogFolder";
            }
            else
            {
                Folder = FolderName;
            }

            if (Log == string.Empty && LogName == string.Empty)
            {
                Log = "LogFile";
            }
            else
            {
                Log = LogName;
            }

            if (Directory.Exists(filePath + "\\" + Folder))
            {

            }
            else
            {
                DirectoryInfo dirInfo = new DirectoryInfo(filePath + "\\" + Folder);
                dirInfo.Create();
            }
            string logFilePath = filePath + "\\" + Folder + "\\" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + Log + ".log";
            using (StreamWriter sw = new StreamWriter(logFilePath, true, Encoding.UTF8))
            {
                StringBuilder sb = new StringBuilder();
                //sb.AppendFormat("-------------------------------");
                //sb.Append(Environment.NewLine);
                sb.Append(tag);
                sb.Append(Environment.NewLine);
                //sb.Append("-------------------------------");
                //sb.Append(Environment.NewLine);
                //sw.WriteLine(DateTime.Now + " | " + tag);
                sw.WriteLine(sb.ToString());
                sw.WriteLine();
            }
        }

        public static void DataLog(string data)
        {
            lock (obj)
            {
                string path = System.IO.Directory.GetCurrentDirectory() + @"\操作日志\";
                if (System.IO.Directory.Exists(path) == false)
                {
                    try
                    {
                        System.IO.Directory.CreateDirectory(path);
                    }
                    catch (Exception ex)
                    {

                    }
                }
                path += DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                using (FileStream fs = new FileStream(path, FileMode.OpenOrCreate))
                {
                    fs.Seek(fs.Length, SeekOrigin.Begin);
                    using (StreamWriter sw = new StreamWriter(fs, Encoding.Default))
                    {
                        fs.Seek(0, SeekOrigin.End);
                        string now = DateTime.Now.ToString();
                        sw.WriteLine(now + "：" + data);
                        sw.Flush();
                        sw.Close();
                    }
                    fs.Close();
                }
            }
        }

        static object obj = new object();
        public static void DataLog(Exception ex)
        {
            lock (obj)
            {
                string path = System.IO.Directory.GetCurrentDirectory() + @"\异常日志\";
                if (System.IO.Directory.Exists(path) == false)
                {
                    System.IO.Directory.CreateDirectory(path);
                }
                path += DateTime.Now.ToString("yyyy-MM-dd") + ".txt";
                FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
                fs.Seek(0, SeekOrigin.End);
                StreamWriter writer = new StreamWriter(fs, Encoding.Default);
                writer.WriteLine(GetExceptionMsg(ex, string.Empty));
                writer.Flush();
                fs.Close();
            }
            
        }

        /// <summary>
        /// 生成自定义异常消息
        /// </summary>
        /// <param name="ex">异常对象</param>
        /// <param name="backStr">备用异常消息：当ex为null时有效</param>
        /// <returns>异常字符串文本</returns>
        static string GetExceptionMsg(Exception ex, string backStr)
        {
            StringBuilder sb = new StringBuilder();
            sb.AppendLine("****************************异常文本****************************");
            sb.AppendLine("【出现时间】：" + DateTime.Now.ToString());
            if (ex != null)
            {
                sb.AppendLine("【异常类型】：" + ex.GetType().Name);
                sb.AppendLine("【异常信息】：" + ex.Message);
                sb.AppendLine("【堆栈调用】：" + ex.StackTrace);

                sb.AppendLine("【异常方法】：" + ex.TargetSite);

            }
            else
            {
                sb.AppendLine("【未处理异常】：" + backStr);
            }
            sb.AppendLine("***************************************************************");
            return sb.ToString();
        }
    }
}
